package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.response.MakeMoneyIndexResponseV3;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;

/**
 * Created by Alan on 2018/7/25.
 */

public class MakeMoneyV3Adapter extends BaseAdapter {

    private Activity mContext;
    private List<MakeMoneyIndexResponseV3.MyInfoBean> list;

    public MakeMoneyV3Adapter(Activity mContext, List<MakeMoneyIndexResponseV3.MyInfoBean> list) {
        this.mContext = mContext;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public MakeMoneyIndexResponseV3.MyInfoBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        MyViewHolder myViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_makemoney_fun_view, null);

            myViewHolder = new MyViewHolder();
            myViewHolder.itemMakeMoneyRootRl = convertView.findViewById(R.id.itemMakeMoneyRootRl);
            myViewHolder.itemMakeMoneyLl = convertView.findViewById(R.id.itemMakeMoneyLl);
            myViewHolder.itemMakeMoneyIv = convertView.findViewById(R.id.itemMakeMoneyIv);
            myViewHolder.itemMakeMoneyInnerNotTv = convertView.findViewById(R.id.itemMakeMoneyInnerNotTv);
            myViewHolder.itemMakeMoneyInnerTv = convertView.findViewById(R.id.itemMakeMoneyInnerTv);
            myViewHolder.itemMakeMoneyTv = convertView.findViewById(R.id.itemMakeMoneyTv);
            myViewHolder.itemMakeMoneyTipTv = convertView.findViewById(R.id.itemMakeMoneyTipTv);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        if (list.get(position).getIncome()>0) {
            myViewHolder.itemMakeMoneyInnerNotTv.setVisibility(View.GONE);
            myViewHolder.itemMakeMoneyInnerTv.setVisibility(View.VISIBLE);
            myViewHolder.itemMakeMoneyInnerTv.setText("+ "+list.get(position).getIncome());
        } else {
            myViewHolder.itemMakeMoneyLl.setBackgroundResource(R.drawable.item_makemoney_not_bg);
            myViewHolder.itemMakeMoneyInnerNotTv.setVisibility(View.VISIBLE);
            myViewHolder.itemMakeMoneyInnerTv.setVisibility(View.GONE);
        }
        myViewHolder.itemMakeMoneyTv.setText(list.get(position).getName());

        setImg(myViewHolder.itemMakeMoneyLl, myViewHolder.itemMakeMoneyIv, list.get(position));
        String tips = list.get(position).getMaxIncome();
        if (StringUtils.isEmpty(tips)) {
            myViewHolder.itemMakeMoneyTipTv.setVisibility(View.INVISIBLE);
        } else {
            myViewHolder.itemMakeMoneyTipTv.setVisibility(View.VISIBLE);
            myViewHolder.itemMakeMoneyTipTv.setText(tips);
        }

        final MyViewHolder finalMyViewHolder = myViewHolder;
        myViewHolder.itemMakeMoneyRootRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AccountUtil.getInstance().getUidNotToLogin(mContext) == 0) return;
                finalMyViewHolder.itemMakeMoneyTipTv.setVisibility(View.INVISIBLE);
//                if (list.get(position).getType()==3) {
//                    mContext.startActivity(new Intent(mContext, TreviFountainListActivity.class));
//                    return;
//                }
                IntentUtil.getInstance().intentTo(mContext, list.get(position).getJumpInfo());
            }
        });

        return convertView;
    }

    private void setImg(LinearLayout itemMakeMoneyLl, ImageView itemMakeMoneyIv, MakeMoneyIndexResponseV3.MyInfoBean myInfoBean) {
        int bg = -1;
        int icon = -1;
        if (myInfoBean.getIncome()>0) {
            if (myInfoBean.getType()==1) {
                bg = R.drawable.item_makemoney_consummate_bg;
                icon = R.mipmap.ic_makemoney_consummate;
            } else if (myInfoBean.getType()==2) {
                bg = R.drawable.item_makemoney_comment_bg;
                icon = R.mipmap.ic_makemoney_comment;
            } else if (myInfoBean.getType()==3) {
                bg = R.drawable.item_makemoney_trevi_bg;
                icon = R.mipmap.ic_makemoney_trevi;
            } else if (myInfoBean.getType()==4) {
                bg = R.drawable.item_makemoney_share_bg;
                icon = R.mipmap.ic_makemoney_share;
            } else if (myInfoBean.getType()==5) {
                bg = R.drawable.item_makemoney_divide_bg;
                icon = R.mipmap.ic_makemoney_divide;
            } else if (myInfoBean.getType()==6) {
                bg = R.drawable.item_makemoney_new_bg;
                icon = R.mipmap.ic_makemoney_new;
            }
            icon = R.mipmap.bg_item_finish_money;
            bg = R.mipmap.bg_item_finish;
        } else {
            bg = R.drawable.item_makemoney_not_bg;
            if (myInfoBean.getType()==1) {
                icon = R.mipmap.ic_makemoney_consummate_not;
            } else if (myInfoBean.getType()==2) {
                icon = R.mipmap.ic_makemoney_comment_not;
            } else if (myInfoBean.getType()==3) {
                icon = R.mipmap.ic_makemoney_trevi_not;
            } else if (myInfoBean.getType()==4) {
                icon = R.mipmap.ic_makemoney_share_not;
            } else if (myInfoBean.getType()==5) {
                icon = R.mipmap.ic_makemoney_divide_not;
            } else if (myInfoBean.getType()==6) {
                icon = R.mipmap.ic_makemoney_new_not;
            }
        }
        itemMakeMoneyLl.setBackgroundResource(bg);
        itemMakeMoneyIv.setImageResource(icon);
    }

    public class MyViewHolder {
        private RelativeLayout itemMakeMoneyRootRl;
        private LinearLayout itemMakeMoneyLl;
        private ImageView itemMakeMoneyIv;
        private TextView itemMakeMoneyInnerNotTv;
        private TextView itemMakeMoneyInnerTv;
        private TextView itemMakeMoneyTv;
        private TextView itemMakeMoneyTipTv;
    }
}
